<jsp:useBean id="actions" scope="application" class="DataAccess.Actions"/>
<%@page import="com.lowagie.text.pdf.PdfPTable"%>
<%@page import="com.lowagie.text.Paragraph"%>
<%@page import="com.lowagie.text.pdf.PdfPCell"%>
<%@page import="java.sql.*"%>  
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="com.lowagie.text.*"%>
<%@page import="com.lowagie.text.pdf.*"%>
<%
//
// Template JSP file for iText
// by Tal Liron
//
//String sql = request.getParameter("query");


response.setContentType( "application/pdf" );

// step 1: creation of a document-object
Document document = new Document();

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a temporary buffer

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance( document, buffer );

// step 3: we open the document 
document.open();



// Inserire connessione a DB e Ciclo
//String sql="select abbonati.nominativo as Nominativo,abbonati.indirizzo as Indirizzo, abbonati.cap as CAP, citta.nome_citta as Comune, province.sigla_provincia as Provincia, tipologie.descrizione_tipologia as Tipologia from abbonati, citta, province, tipologie where abbonati.id_citta = citta.id_citta and citta.id_provincia = province.id_provincia and abbonati.id_tipologia = tipologie.id_tipologia and abbonati.id_tipologia=7";

try {
            Class.forName(actions.getDriverString());
            Connection cn = DriverManager.getConnection (actions.getConnectionString());
            java.sql.Statement stmt = cn.createStatement();
            //String sql=Query;
            //String sql=actions.getCurrentQuery();
            ResultSet rs = stmt.executeQuery(actions.getCurrentQuery());
            
            ResultSetMetaData rsm = rs.getMetaData();
            int col = rsm.getColumnCount();
            //System.out.print(col);
            //html_output += "<tr><td></td>";
            
            Font font_normal = new Font(Font.HELVETICA, 6, Font.NORMAL);
            Font font_bold = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD);
            
            
            PdfPTable table = new PdfPTable(col);
	    //PdfPCell cell = new PdfPCell(new Paragraph("Titolo Tabella",font));
            //cell.setColspan(col);
	    //table.addCell(cell);
            
            for(int i=1; i<=col; i++) {
               // html_output += "<td><STRONG>" + rsm.getColumnName(i) + "</strong></td>";
               //document.add(new Paragraph(rsm.getColumnName(i)));
               //table.addCell(rsm.getColumnName(i));
                table.addCell(new PdfPCell(new Paragraph(rsm.getColumnName(i),font_bold)));
            }
            //html_output += "</tr>";
            // Read the data
            
            while(rs.next()) {

                for(int j=1; j<=col; j++) {
                    // Here is the tricky part which I did not figure yet. For now, I am reading in all as Strings.
                   // row.add(rs.getObject(j).toString());
                    //html_output += "<td>" + rs.getObject(j).toString() + "</td>";
                    //html_output += "<td>" + rs.getString(j) + "</td>";
                    //document.add(rsm.getColumnName(i));
                    //document.add(new Paragraph(rs.getString(j)));
                    //table.addCell(rs.getString(j));
                    table.addCell(new Paragraph(rs.getString(j),font_normal));
                }

            }
            //html_output += "</table><br></fieldset>";
            rs.close();
            stmt.close();
            cn.close();
            
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setWidthPercentage(100);
            
            document.add(table);
        } catch (Exception e) {
           // html_output = showError(e);
           document.add(new Paragraph(e.getMessage()));
        }



// step 4: we add a paragraph to the document
//document.add(new Paragraph("Hello World"));

// step 5: we close the document
document.close();

// step 6: we output the writer as bytes to the response output
DataOutput output = new DataOutputStream( response.getOutputStream() );

byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for( int i = 0; i < bytes.length; i++ ) { output.writeByte( bytes[i] ); }
%>